define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	var needSetSpot = [];

	var Base = view.container.extend({
		init:function(config){
			this.$channels = pubjs.config('exchange');
			this.$defaultChannel = this.$channels && this.$channels[0].id;
			config = pubjs.conf(config, {
				'tplFile':	'admin/tpl/deal/edit.tpl',
				'readonly': {
					'ALL': false          // 禁用全部
					,'name': false
					,'dealId': false
					,'status': false
				},
				'channelId': this.$defaultChannel,
				'view_model': {
					'headTitle': ''
					,'isEdit':false
					,'channelName':''//渠道名称，编辑状态下显示
					,'readonly': {
						'name': false
						,'dealId': false
						,'status': false
						,'AdxPIds': false
						,'price': false
					}
					,view: {
						Time: false	// 有效日期
						,AdxPIds: false	// 广告位ID
						,BackRatio: false	// 广告位ID
					}
					,'isSave': false
					,'status': true
					,'name': ''
					,'dealId': ''
					,'AdxPIds': ''
					//私有底价
					,'PrivateFloor': ''
					//订单价格
					,'Price': ''
					// 返量比例
					,'BackRatio': ''
				},
				urlUpdate: 'deal/update',
				urlAdd: 'deal/add',
				urlInfo: 'deal/info'
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			var c = this.getConfig();

			var vm = this.vm.$;
			var creadonly = c.readonly;
			var vreadonly = vm.readonly;
			this.$head = {
				1: LANG('首选交易订单')
				,2: LANG('私下竞价订单')
				,3: LANG('PDB订单')
			};

			util.each(vreadonly.$model, function(item, key) {
				vreadonly[key] = creadonly.ALL || creadonly[key];
			});

			var config = {
				// 表单取消按钮
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20'
				}
				// 表单保存按钮
				,'save': {
					'value': LANG('保存'),
					'class': 'btn btn-confirm'
				}
				,channel: {
					'defaultValue': c.channelId
					,'items': self.$channels
					,'reqType': 'websocket'
					,'prefix': 'dealEditChannel'
					,'silent': false
				}
				,channelTip: {
					'width': 300,
					'data': LANG('即不同的AD-Exchange')
				}
				,SpotTip: {
					'width': 300,
					'data': LANG("以分号';'或换行来做分隔广告位Id")
				}
				// 有效日期开始
				,startTime: {
					value: util.date('Y-m-d'),
					buttons: null
				}
				// 有效结束日期
				,endTime: {
					min: util.date('Y-m-d')
				}
			};

			self.$channelId = self.$defaultChannel;
			self.buildFromTemplate(config);
		}
		,afterBuildTemplate: function() {
			this.updateUI();
		}
		,load: function(id){
			var self = this;
			self.$id = id;
			if(id){
				pubjs.loading.show();
				pubjs.mc.send(this.getConfig('urlInfo'), {_id: id}, function(err, data){
					pubjs.loading.hide();
					if(err){
						pubjs.alert(err.message);
						return false;
					}
					self.setData(data);
				})
			}

			return self;
		}
		,validate: function(data){
			if(!data.Name){
				pubjs.alert(LANG('请输入订单名称'));
				return false;
			}
			if(!data.AdxDealId){
				pubjs.alert(LANG('请输入订单ID'));
				return false;
			}
			if(!data.AdxId) {
				pubjs.alert(LANG('请选择相应的渠道'));
				return false;
			}
			if(~needSetSpot.indexOf(data.AdxId)) {
				if(!data.ManualInfo) {
					pubjs.alert(LANG('渠道要求有效日期与广告位ID'));
					return false;
				}
				if(!data.ManualInfo.StartTime) {
					pubjs.alert(LANG('请设置有效日期的开始时间'));
					return false;
				}
			}
			if (!util.isNumber(data.PrivateFloor))
			{
				pubjs.alert(LANG('私有底价请输入数字'));
				return false;
			}

			if (!util.isNumber(data.Price))
			{
				pubjs.alert(LANG('订单价格请输入数字'));
				return false;
			}

			if(data.hasOwnProperty('BackRatio'))
			{
				if(!util.isNumber(data.BackRatio))
				{
					pubjs.alert(LANG('返量比例请输入数字'));
					return false;
				}
			}

			return true;
		}
		,setData: function(data) {
			this.$data = data;
			var self = this
				,vm = self.vm.$;

			vm.name = data.Name;
			vm.status = data.Status == 1;
			vm.dealId = data.AdxDealId;
			if(vm.isEdit) {
				vm.channelName = data.channelName;
			}
			self.$channelId = +data.AdxId;
			self.$.channel.setValue(self.$channelId);
			self.$.client.setValue(data.ClientsInfo || []);
			if(~needSetSpot.indexOf(self.$channelId)) {
				vm.AdxPIds = data.ManualInfo.AdxPIds.join(';');
				var st = data.ManualInfo.StartTime;
				var et = data.ManualInfo.EndTime;
				st = st ? util.date('Ymd', st) : st;
				et = et ? util.date('Ymd', et) : et;
				self.$.startTime.setData(st);
				self.$.endTime.setData(et);
			}

			//私有底价
			vm.PrivateFloor = data.PrivateFloor || '';

			//订单价格
			if (data.DealInfo)
			{
				vm.Price = data.DealInfo.Price || 0;
			}

			vm.BackRatio = data.BackRatio || '';
			self.updateUI();
			return self;
		}
		/*
			UI变化的更新.
		 */
		,updateUI: function() {
			var self = this;
			var vm = self.vm.$;
			var view = vm.view;
			// 百度渠道
			if(~needSetSpot.indexOf(+self.$channelId)) {
				view.Time = true;
				view.AdxPIds = true;
			}
			else {
				view.Time = false;
				view.AdxPIds = false;
			}
		}
		,onRadioGroupClick: function(ev) {
			if(ev.name == 'channel') {
				this.$channelId = +ev.param;
				this.updateUI();
			}
		}
		,getData: function(){
			var self = this
				,data
				,vm = self.vm.$;

			data = {
				_id: self.$id
				,Name: vm.name
				,AdxDealId: vm.dealId
				,Status: vm.status ? 1: 2
				,Clients: self.$.client.getValue()
				,Type: self.$type
				,AdxId: self.$.channel.getValue()
				// 私有底价
				,PrivateFloor: +vm.PrivateFloor
				// 订单价格
				,Price: +vm.Price
			};

			if(~needSetSpot.indexOf(+data.AdxId)) {
				data.ManualInfo = {
					StartTime: self.$.startTime.getData(true)
					,EndTime: self.$.endTime.getData(true)
					,AdxPIds: vm.AdxPIds.replace(/(\n)+|(\r\n)+|(；)/g, ';').split(';')
				}
			}

			if(vm.view.BackRatio)
			{
				data.BackRatio = vm.BackRatio;
			}

			return data;
		}
		,setMode: function(type, isEdit) {
			var self = this;
			self.$type = type || 1;
			var vm = self.vm.$;
			if(isEdit) {
				vm.isEdit = true;
				vm.readonly.dealId = true;
				vm.readonly.status = true;
				//vm.readonly.AdxPIds = true;
				vm.readonly.price = true;
			}
			var head = (isEdit ? LANG('编辑'): LANG('新建')) + self.$head[self.$type];
			self.vm.setItem('headTitle', head);
			self.vm.$.view.BackRatio = type == 3;
			return this;
		}
		,reset: function(){
			this.$type = 1;
			this.$id = 0;
			this.vm.reset();
			this.$.client.reset();
			this.$.channel.setValue(this.$defaultChannel);
			return this;
		}
		,save: function() {
			var self = this;
			var data = self.getData();
			var vm = self.vm.$;
			if(self.validate(data)) {
				pubjs.loading.show();
				var url = vm.isEdit ? 'urlUpdate' : 'urlAdd';
				pubjs.mc.send(self.getConfig(url), data, self.afterSave.bind(self))
			}

			return false;
		}
		,afterSave: function(err, data) {
			pubjs.loading.hide();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			pubjs.controller.navigate('deal');
		}
		,onInputClick: function(ev) {
			if(ev.name == 'save') {
				this.save();
			}
			else if(ev.name == 'cancel') {
				pubjs.controller.navigate('deal');
			}
		}

	});

	exports.base = Base;
});